package cn.iocoder.yudao.module.trade.order.api;

import cn.iocoder.yudao.framework.business.order.pojo.vo.OrderSubmitVO;
import cn.iocoder.yudao.framework.common.exception.KnownServiceException;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.api.product.evaluation.dto.ProductEvaluationCreateReqDTO;
import cn.iocoder.yudao.module.api.trade.order.TradeOrderApi;
import cn.iocoder.yudao.module.api.trade.order.dto.GiftTradeOrderSaveDTO;
import cn.iocoder.yudao.module.api.trade.order.dto.TradeOrderItemRespDTO;
import cn.iocoder.yudao.module.api.trade.order.dto.TradeOrderRespDTO;
import cn.iocoder.yudao.module.api.trade.order.enums.TradeOrderStatusEnum;
import cn.iocoder.yudao.module.api.trade.order.vo.TradeOrderSummaryVO;
import cn.iocoder.yudao.module.trade.aftersale.service.AfterSaleService;
import cn.iocoder.yudao.module.trade.order.convert.TradeOrderConvert;
import cn.iocoder.yudao.module.trade.order.dal.dataobject.TradeOrderItemDO;
import cn.iocoder.yudao.module.trade.order.service.TradeOrderQueryService;
import cn.iocoder.yudao.module.trade.order.service.TradeOrderUpdateService;
import cn.iocoder.yudao.module.trade.order.vo.AppTradeOrderEvaluationCreateReqVO;
import cn.iocoder.yudao.module.trade.order.vo.AppTradeOrderItemEvaluationCreateReqVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.Collection;
import java.util.List;

/**
 * 订单 API 接口实现类
 *
 * @author HUIHUI
 * @author 山野羡民
 */
@Service
@Validated
public class TradeOrderApiImpl implements TradeOrderApi {

    @Resource
    private TradeOrderUpdateService tradeOrderUpdateService;
    @Resource
    private TradeOrderQueryService tradeOrderQueryService;
    @Resource
    private AfterSaleService afterSaleService;

    @Override
    public OrderSubmitVO createGiftTradeOrder(Long userId, GiftTradeOrderSaveDTO saveDTO) {
        return tradeOrderUpdateService.createGiftTradeOrder(userId, saveDTO);
    }

    @Override
    public List<TradeOrderRespDTO> getOrderList(Collection<Long> ids) {
        return TradeOrderConvert.INSTANCE.convertList04(tradeOrderQueryService.getOrderList(ids));
    }

    @Override
    public TradeOrderRespDTO getOrder(Long id) {
        return TradeOrderConvert.INSTANCE.convert(tradeOrderQueryService.getOrder(id));
    }

    @Override
    public TradeOrderRespDTO getByTradeNo(String no) {
        return TradeOrderConvert.INSTANCE.convert(tradeOrderQueryService.getOrderByNo(no));
    }

    @Override
    public void cancelPaidOrder(Long userId, Long orderId, Integer cancelType) {
        tradeOrderUpdateService.cancelPaidOrderByBuyer(userId, orderId, cancelType);
    }

    @Override
    public TradeOrderItemRespDTO getOrderItem(Long id) {
        TradeOrderItemDO orderItem = tradeOrderQueryService.getOrderItem(id);
        return BeanUtils.toBean(orderItem, TradeOrderItemRespDTO.class);
    }

    @Override
    public List<TradeOrderItemRespDTO> getOrderItemsByOrderId(Long orderId) {
        List<TradeOrderItemDO> list = tradeOrderQueryService.getOrderItemListByOrderId(orderId);
        return BeanUtils.toBean(list, TradeOrderItemRespDTO.class);
    }

    @Override
    public Long createOrderCommentByMember(Long userId, ProductEvaluationCreateReqDTO dto) {
        if (dto.getOrderItemId() != null && dto.getOrderItemId() > 0) {
            AppTradeOrderItemEvaluationCreateReqVO reqVO = BeanUtils.toBean(dto, AppTradeOrderItemEvaluationCreateReqVO.class);
            return tradeOrderUpdateService.createOrderItemCommentByMember(userId, reqVO);
        }
        if (dto.getOrderId() != null && dto.getOrderId() > 0) {
            AppTradeOrderEvaluationCreateReqVO reqVO = BeanUtils.toBean(dto, AppTradeOrderEvaluationCreateReqVO.class);
            return tradeOrderUpdateService.createOrderCommentByMember(userId, reqVO);
        }
        throw new KnownServiceException("订单编号或订单项编号不能为空");
    }

    @Override
    public TradeOrderSummaryVO getOrderSummaryByShopId(Long shopId) {
        TradeOrderSummaryVO countVO = new TradeOrderSummaryVO();
        countVO.setPaidCount(tradeOrderQueryService.getOrderCountByShopId(shopId, TradeOrderStatusEnum.PAID.getStatus(), null));
        countVO.setDoingCount(tradeOrderQueryService.getOrderCountByShopId(shopId, TradeOrderStatusEnum.DOING.getStatus(), null));
        countVO.setCompletedCount(tradeOrderQueryService.getOrderCountByShopId(shopId, TradeOrderStatusEnum.COMPLETED.getStatus(), null));
        countVO.setRefundCount(afterSaleService.getApplyingAfterSaleCountByShopId(shopId, true));
        return countVO;
    }

    @Override
    public TradeOrderItemRespDTO createVirtualOrderComment(Long userId, Long skuId) {
        throw new KnownServiceException("虚拟评价暂不支持");
//        tradeOrderUpdateService.createOrder();
//        tradeOrderUpdateService.createOrderCommentByMember();
    }

    @Override
    public boolean isNewShopCustomer(Long shopId, Long userId) {
        return tradeOrderQueryService.isNewShopCustomer(shopId, userId);
    }

}
